#include "Color.h"

using namespace ColorTree;
Color::Color(uint8_t r, uint8_t g, uint8_t b)
{
    color[0] = r;
    color[1] = g;
    color[2] = b;
    ColorToBit();
}

Color::Color(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7)
{
    colorBit[0] = b0;
    colorBit[1] = b1;
    colorBit[2] = b2;
    colorBit[3] = b3;
    colorBit[4] = b4;
    colorBit[5] = b5;
    colorBit[6] = b6;
    colorBit[7] = b7;
    BitToColor();
}

void Color::ColorToBit()
{
    colorBit[0] =
            ((color[0] & (unsigned) 0x80) >> (unsigned) 5) +
            ((color[1] & (unsigned) 0x80) >> (unsigned) 6) +
            ((color[2] & (unsigned) 0x80) >> (unsigned) 7);
    colorBit[1] =
            ((color[0] & (unsigned) 0x40) >> (unsigned) 4) +
            ((color[1] & (unsigned) 0x40) >> (unsigned) 5) +
            ((color[2] & (unsigned) 0x40) >> (unsigned) 6);
    colorBit[2] =
            ((color[0] & (unsigned) 0x20) >> (unsigned) 3) +
            ((color[1] & (unsigned) 0x20) >> (unsigned) 4) +
            ((color[2] & (unsigned) 0x20) >> (unsigned) 5);
    colorBit[3] =
            ((color[0] & (unsigned) 0x10) >> (unsigned) 2) +
            ((color[1] & (unsigned) 0x10) >> (unsigned) 3) +
            ((color[2] & (unsigned) 0x10) >> (unsigned) 4);
    colorBit[4] =
            ((color[0] & (unsigned) 0x08) >> (unsigned) 1) +
            ((color[1] & (unsigned) 0x08) >> (unsigned) 2) +
            ((color[2] & (unsigned) 0x08) >> (unsigned) 3);
    colorBit[5] =
            ((color[0] & (unsigned) 0x04) >> (unsigned) 0) +
            ((color[1] & (unsigned) 0x04) >> (unsigned) 1) +
            ((color[2] & (unsigned) 0x04) >> (unsigned) 2);
    colorBit[6] =
            ((color[0] & (unsigned) 0x02) << (unsigned) 1) +
            ((color[1] & (unsigned) 0x02) >> (unsigned) 0) +
            ((color[2] & (unsigned) 0x02) >> (unsigned) 1);
    colorBit[7] =
            ((color[0] & (unsigned) 0x01) << (unsigned) 2) +
            ((color[1] & (unsigned) 0x01) << (unsigned) 1) +
            ((color[2] & (unsigned) 0x01) >> (unsigned) 0);
}

void Color::BitToColor()
{
    color[0] =
            ((colorBit[0] & (unsigned) 0x04) << (unsigned) 5) +
            ((colorBit[1] & (unsigned) 0x04) << (unsigned) 4) +
            ((colorBit[2] & (unsigned) 0x04) << (unsigned) 3) +
            ((colorBit[3] & (unsigned) 0x04) << (unsigned) 2) +
            ((colorBit[4] & (unsigned) 0x04) << (unsigned) 1) +
            ((colorBit[5] & (unsigned) 0x04) << (unsigned) 0) +
            ((colorBit[6] & (unsigned) 0x04) >> (unsigned) 1) +
            ((colorBit[7] & (unsigned) 0x04) >> (unsigned) 2);
    color[1] =
            ((colorBit[0] & (unsigned) 0x02) << (unsigned) 6) +
            ((colorBit[1] & (unsigned) 0x02) << (unsigned) 5) +
            ((colorBit[2] & (unsigned) 0x02) << (unsigned) 4) +
            ((colorBit[3] & (unsigned) 0x02) << (unsigned) 3) +
            ((colorBit[4] & (unsigned) 0x02) << (unsigned) 2) +
            ((colorBit[5] & (unsigned) 0x02) << (unsigned) 1) +
            ((colorBit[6] & (unsigned) 0x02) << (unsigned) 0) +
            ((colorBit[7] & (unsigned) 0x02) >> (unsigned) 1);
    color[2] =
            ((colorBit[0] & (unsigned) 0x01) << (unsigned) 7) +
            ((colorBit[1] & (unsigned) 0x01) << (unsigned) 6) +
            ((colorBit[2] & (unsigned) 0x01) << (unsigned) 5) +
            ((colorBit[3] & (unsigned) 0x01) << (unsigned) 4) +
            ((colorBit[4] & (unsigned) 0x01) << (unsigned) 3) +
            ((colorBit[5] & (unsigned) 0x01) << (unsigned) 2) +
            ((colorBit[6] & (unsigned) 0x01) << (unsigned) 1) +
            ((colorBit[7] & (unsigned) 0x01) << (unsigned) 0);
}


